
--- 获得你与“绯花”角色累计造成伤害的十位数
---@param player Player
local function getFeiHuaCount(player)
  local room = Fk:currentRoom()
  local count = player:getMark("emo__feihua_damage")
  -- 注意绯花角色可能含有自己
  for _, pid in ipairs(player:getTableMark("emo__feihua_tar")) do
    local to = room:getPlayerById(pid)
    if to ~= player then
      count = count + to:getMark("emo__feihua_damage")
    end
  end
  return count // 10
end

local skel = fk.CreateSkill {
  name = "emo__feihuajl",
  dynamic_desc = function (self, player, lang)
    return "emo__feihuajl_dyn:" .. getFeiHuaCount(player)
  end,
}

Fk:loadTranslationTable{
  ["emo__feihuajl"] = "绯花剑落",
  [":emo__feihuajl"] = "当你使用牌，你可以额外结算X次。当你弃置牌后，你可以重铸至多X张牌（X为你与“绯花”角色累计造成伤害的十位数）。",

  [":emo__feihuajl_dyn"] = "当你使用牌，你可以额外结算X次。当你弃置牌后，你可以重铸至多X张牌（X={1}，为你与“绯花”角色累计造成伤害的十位数）。",

  ["#emo__feihuajl-extra"] = "绯花剑落：你可以令 %arg 额外结算 %arg2 次",
  ["#emo__feihuajl-recast"] = "绯花剑落：重铸至多 %arg 张牌",

  ["$emo__feihuajl1"] = "今天将会是谁在我的剑下绽放呢？",
  ["$emo__feihuajl2"] = "这一地的花瓣，真是美得无与伦比。",
}

-- 添加伤害记录器
skel:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#emo__feihua_record")
end)

skel:addEffect(fk.CardUsing, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.tos and #data.tos > 0 then
      return getFeiHuaCount(player) > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name,
      prompt = "#emo__feihuajl-extra:::" .. data.card:toLogString() .. ":" .. getFeiHuaCount(player)
    })
  end,
  on_use = function (self, event, target, player, data)
    data.additionalEffect = (data.additionalEffect or 0) + getFeiHuaCount(player)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and not player:isNude() and getFeiHuaCount(player) > 0 then
      for _, move in ipairs(data) do
        if move.moveReason == fk.ReasonDiscard and move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local n = getFeiHuaCount(player)
    local cards = player.room:askToCards(player, {
      min_num = 1, max_num = n, skill_name = skel.name, include_equip = true, cancelable = true,
      prompt = "#emo__feihuajl-recast:::" .. n
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:recastCard(event:getCostData(self).cards, player, skel.name)
  end,
})


return skel
